$(function () {
    $("#jqGrid").jqGrid({
        url: '../tapideployinfo/list',
        datatype: "json",
        colModel: [			
			
			{ label: '企业id', name: 'uuid', index: 'uuid', width: 120 }, 			
			{ label: 'ip', name: 'ip', index: 'ip', width: 120 }, 			
			{ label: '端口', name: 'port', index: 'port', width: 80 }, 			
			{ label: '集团名称', name: 'name', index: 'name', width: 120 }, 			
			{ label: '启用状态', name: 'status', index: 'status', width: 80,
				formatter:function(cellvalue, options, rowObject){
					if(cellvalue==1)
						return "已启用";
					else
						return "未启用";
				} 				
			},
			{ label: 'corpId', name: 'ekbCorpId', index: 'ekb_corp_id', width: 120 }, 			 			
			{ label: 'powerCode', name: 'powerCode', index: 'power_code', width: 80 }, 
			{ label: '地址前缀', name: 'ekbPrefix', index: 'ekb_prefix', width: 80 }, 
			{ label: '授权状态', name: 'tokenStatus', index: 'token_status', width: 80,
				formatter:function(cellvalue, options, rowObject){
					if(cellvalue==1)
						return "已授权";
					else
						return "未授权";
				} 				
			},
			{ label: '备注', name: 'remark', index: 'remark', width: 185 }
			
        ],
		viewrecords: true,
        height: 385,
        width:1090,
        shrinkToFit:false,  
        autoScroll: true, 
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
//        autowidth:true,
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    $("#jqGrid").setGroupHeaders(
    	{
    		useColSpanStyle:true,
    		groupHeaders:[
    			{"numberOfColumns":5,"titleText":"云财务下游服务","startColumnName":"uuid"},
    			{"numberOfColumns":3,"titleText":"易快报","startColumnName":"ekbCorpId"}
    		]
    	}	
    );
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		showVerifyCode: false,
		title: null,
		tApiDeployinfo: {},
		verifyCode:"",
		selectedId:""
	},
	methods: {
		query: function () {
			vm.reload();
		},
		enable: function(){
			var id = getSelectedRow();
			if(!id)
				return;
			var url = "../tapideployinfo/enable/"+id;
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data:{},
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		disable: function(){
			var id = getSelectedRow();
			if(!id)
				return;
			var url = "../tapideployinfo/disable/"+id;
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data:{},
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		gettoken: function(){
			vm.showList = false;
			vm.title = "授权-获取验证码";
			vm.showVerifyCode = true;
			var id = getSelectedRow();
			if(!id)
				return;
			vm.selectedId = id
		},
		auth: function(){
			if(!vm.verifyCode){
				alert("验证码不能为空");
			}
			var url = "../tapideployinfo/ekbauth/"+vm.verifyCode+"/"+vm.selectedId;
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data:{},
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		testservice: function(){
			var id = getSelectedRow();
			if(!id)
				return;
			var url = "../tapideployinfo/testekbservice/"+id;
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data:{},
			    success: function(r){
			    	if(r.code === 0){
			    		var res = JSON.parse(r.data);
						alert('操作成功，返回企业信息如下：'+res.value.name);
					}else{
						alert(r.msg);
					}
				}
			});
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.tApiDeployinfo = {};
			vm.tApiDeployinfo.ekbPrefix = "dd2";
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)

			
		},
		saveOrUpdate: function (event) {
			var url = vm.tApiDeployinfo.id == null ? "../tapideployinfo/save" : "../tapideployinfo/update";
			if(!vm.tApiDeployinfo.uuid){
				alert('企业id为空');
				return;
			}
			if(!vm.tApiDeployinfo.ip){
				alert('下游服务ip为空');
				return;
			}
			if(!vm.tApiDeployinfo.port){
				alert('下游服务端口为空');
				return;
			}
			if(!vm.tApiDeployinfo.name){
				alert('下游服务集团名称为空');
				return;
			}
			if(!vm.tApiDeployinfo.ekbCorpId){
				alert('易快报企业id(corpId)为空');
				return;
			}
			if(!vm.tApiDeployinfo.powerCode){
				alert('第三方功能授权powerCode为空');
				return;
			}
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.tApiDeployinfo),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../tapideployinfo/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../tapideployinfo/info/"+id, function(r){
                vm.tApiDeployinfo = r.tApiDeployinfo;
                vm.tApiDeployinfo.ekbPrefix = "dd2";
            });
		},
		reload: function (event) {
			vm.showList = true;
			vm.showVerifyCode = false;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});